package com.baoy.cloud.xxx.sso.shiro.matcher;

import com.baoy.cloud.xxx.common.utils.Encodes;
import com.baoy.cloud.xxx.sso.utils.EncryptUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.util.ByteSource;


public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String jspPwd = new String(usernamePasswordToken.getPassword());

        SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) info;
        ByteSource salt = authenticationInfo.getCredentialsSalt();

        jspPwd = EncryptUtils.entryptPassword(jspPwd, Encodes.decodeHex(salt.toHex()));
        Object dbPwd = getCredentials(info);
        return equals(jspPwd, dbPwd);
    }

}
